import React, {Component, Fragment} from 'react';
import {Input, Table} from 'antd';
import "./UpperPartOfTheForm.css"

class CKslyclhxfxbgd extends Component {

    constructor(props) {
        super(props);
        this.state = {
            Time: [],//第一列的时间变化自动控制
            Data: this.props.upperData,//原始填写的数据
            passRate: [],//合格率
            average: [],//平均
            standard: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,]

        }
    }


    componentDidMount() {
        const allTime = [
            ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00'],
            ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00'],
            ['16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
        ];
        const standard = [];
        standard[12] = this.props.standard[0];
        standard[13] = this.props.standard[1];
        standard[14] = this.props.standard[2];
        this.setState({
            Time: [...allTime[this.props.timeChose]],
            standard: standard
        });
    }

    componentWillReceiveProps(nextProps) {
        const allTime = [
            ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00'],
            ['8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00'],
            ['16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
        ];
        const standard = [];
        standard[12] = nextProps.standard[0];
        standard[13] = nextProps.standard[1];
        standard[14] = nextProps.standard[2];
        this.setState({
            Time: [...allTime[nextProps.timeChose]],
            Data: nextProps.upperData,
            standard: standard
        });
        this.updataData_Initial(standard);
    }

    /***
     * 进行底部的合格率、平均数的计算更新以及是否正常的判断
     **/
    updataData_Initial(standard) {
        let temp1 = 0;//计算均值和合格率的临时数据
        let temp2 = 0;
        let temp3 = 0;
        let str = '';//计算合格比率的临时数据
        let inputCount0 = [], inputCount8 = [], inputCount16 = [];//各班次输入的总数
        let passCount0 = [], passCount8 = [], passCount16 = [];//各班次的合格数
        let passRate0 = [], passRate8 = [], passRate16 = [];//各班次的合格率
        let ratio0 = [], ratio8 = [], ratio16 = [];//各班次的合格比率
        //计算合格率部分
        for (let i = 0; i < 15; i++) {//i是列
            inputCount0[i] = 0;//0点班输入的总数
            passCount0[i] = 0;//0点班合格数
            inputCount8[i] = 0;//8点班输入的总数
            passCount8[i] = 0;//8点班合格数
            inputCount16[i] = 0;//16点班输入的总数
            passCount16[i] = 0;//16点班合格数
            for (let j = 0; j <= 7; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount0[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount0[i]++;
                    }
                }
                if (inputCount0[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount0[i] + '\/' + inputCount0[i];
                    if (isNaN(temp1 = passCount0[i] / inputCount0[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio0[i] = str;
                passRate0[i] = temp1;
            }
            for (let j = 8; j <= 15; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount8[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount8[i]++;
                    }
                }
                if (inputCount8[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount8[i] + '\/' + inputCount8[i];
                    if (isNaN(temp1 = passCount8[i] / inputCount8[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio8[i] = str;
                passRate8[i] = temp1;
            }
            for (let j = 16; j <= 23; j++) {//j是行
                if (!isNaN(this.state.Data[j]['shuju'][i]) && (this.state.Data[j]['shuju'][i] != null)) {
                    inputCount16[i]++;
                    if (this.state.Data[j]['shuju'][i] <= standard[i]) {
                        passCount16[i]++;
                    }
                }
                if (inputCount16[i] === 0) {
                    str = null;
                    temp1 = null;
                }
                else {
                    str = passCount16[i] + '\/' + inputCount16[i];
                    if (isNaN(temp1 = passCount16[i] / inputCount16[i])) {
                        temp1 = null;
                    }
                    else {
                        temp1 = Number(temp1 * 100).toFixed(1) + '%';
                    }
                }
                ratio16[i] = str;
                passRate16[i] = temp1;
            }
            let ratio = ratio0.concat(ratio8);
            ratio = ratio.concat(ratio16);
            let passRate = passRate0.concat(passRate8);
            passRate = passRate.concat(passRate16);
            this.setState({
                ratio: ratio,
                passRate: passRate,
            })
        }
        //计算均值的部分
        // let average0 = [], average8 = [], average16 = [];//各班次的合格率
        // for (let i = 0; i < 15; i++) {
        //     temp1 = 0;
        //     temp2 = 0;
        //     temp3 = 0;
        //     if (inputCount0[i] === 0) {
        //         temp1 = null;
        //     }
        //     else {
        //         for (let j = 0; j < 8; j++) {
        //             if (!isNaN(this.state.Data[j]['shuju'][i])) {
        //                 temp1 += this.state.Data[j]['shuju'][i];
        //             }
        //         }
        //         if (isNaN(temp1 = temp1 / inputCount0[i])) {
        //             temp1 = 0;
        //         }
        //         temp1 = temp1.toFixed(3);
        //     }
        //     average0[i] = temp1;
        //     if (inputCount8[i] === 0) {
        //         temp2 = null;
        //     }
        //     else {
        //         for (let j = 8; j < 16; j++) {
        //             if (!isNaN(this.state.Data[j]['shuju'][i])) {
        //                 temp2 += this.state.Data[j]['shuju'][i];
        //             }
        //         }
        //         if (isNaN(temp2 = temp2 / inputCount8[i])) {
        //             temp2 = 0;
        //         }
        //         temp2 = temp2.toFixed(3);
        //     }
        //     average8[i] = temp2;
        //     if (inputCount16[i] === 0) {
        //         temp3 = null;
        //     }
        //     else {
        //         for (let j = 16; j < 24; j++) {
        //             if (!isNaN(this.state.Data[j]['shuju'][i])) {
        //                 temp3 += this.state.Data[j]['shuju'][i];
        //             }
        //         }
        //         if (isNaN(temp3 = temp3 / inputCount16[i])) {
        //             temp3 = 0;
        //         }
        //         temp3 = temp3.toFixed(3);
        //     }
        //     average16[i] = temp3;
        // }
        // let average = average0.concat(average8);
        // average = average.concat(average16);
        // this.setState({
        //     average: average
        // });
    }

    render() {

        // 表头
        const columns = [
            {
                title: '时间',
                key: 'time',
                dataIndex: 'time',
                fixed: 'left',
                width: 100
            },
            {
                title: '附着水',
                dataIndex: 'FuZhuoShui_list',
                key: 'FuZhuoShui_list',
            },
            {
                title: '不溶物',
                key: 'BuRongWu_list',
                dataIndex: 'BuRongWu_list',
            },
            {
                title: 'LL/%',
                key: 'LL_list',
                dataIndex: 'LL_list',
            },
            {
                title: 'SiO2/%',
                key: 'ErYangHG_list',
                dataIndex: 'ErYangHG_list',
            },
            {
                title: 'Al2O3/%',
                key: 'YangHuaL_list',
                dataIndex: 'YangHuaL_list',
            },
            {
                title: 'Fe2O3/%',
                key: 'YangHuaT_list',
                dataIndex: 'YangHuaT_list',
            },
            {
                title: 'CaO/%',
                key: 'YangHuaG_list',
                dataIndex: 'YangHuaG_list',
            },
            {
                title: 'MgO/%',
                key: 'YangHuaMei_list',
                dataIndex: 'YangHuaMei_list',
            },
            {
                title: 'MnO/%',
                key: 'YangHuaMeng_list',
                dataIndex: 'YangHuaMeng_list',
            },
            {
                title: 'TO/%',
                key: 'TO_list',
                dataIndex: 'TO_list',
            },
            {
                title: 'SO3/%',
                key: 'SanYangHL_list',
                dataIndex: 'SanYangHL_list',
            },
            {
                title: '合计',
                key: 'HeJi',
                dataIndex: 'HeJi',
            },
            {
                title: 'KH/%',
                key: 'KH_list',
                dataIndex: 'KH_list',
            },
            {
                title: 'n/%',
                key: 'n',
                dataIndex: 'n',
            },
            {
                title: 'P/%',
                key: 'P',
                dataIndex: 'P',
            },
            {
                title: '人员',
                key: 'person',
                dataIndex: 'person',
            },
        ];

        const data = [];
        //中间八行的数据输入
        const Data = this.state.Data;
        for (let i = 0; i < 8; i++) {
            let xiaoshi = i + this.props.timeChose * 8;
            const value = Data[xiaoshi]['shuju'];
            data.push(
                {
                    time: this.state.Time[i],
                    FuZhuoShui_list: isNaN(value[0]) ? null : value[0],
                    BuRongWu_list: isNaN(value[1]) ? null : value[1],
                    LL_list: isNaN(value[2]) ? null : value[2],
                    ErYangHG_list: isNaN(value[3]) ? null : value[3],
                    YangHuaL_list: isNaN(value[4]) ? null : value[4],
                    YangHuaT_list: isNaN(value[5]) ? null : value[5],
                    YangHuaG_list: isNaN(value[6]) ? null : value[6],
                    YangHuaMei_list: isNaN(value[7]) ? null : value[7],
                    YangHuaMeng_list: isNaN(value[8]) ? null : value[8],
                    TO_list: isNaN(value[9]) ? null : value[9],
                    SanYangHL_list: isNaN(value[10]) ? null : value[10],
                    HeJi: isNaN(value[11]) ? null : value[11],
                    KH_list: isNaN(value[12]) ? null : value[12],
                    n: isNaN(value[13]) ? null : value[13],
                    P: isNaN(value[14]) ? null : value[14],
                    person: Data[xiaoshi]['name']
                }
            )
        }

        //数据的自动处理显示部分
        const xiaoshi = this.props.timeChose * 15;
        data.push(
            {
                time: '平均',
                FuZhuoShui_list: this.state.average[xiaoshi],
                BuRongWu_list: this.state.average[1 + xiaoshi],
                LL_list: this.state.average[2 + xiaoshi],
                ErYangHG_list: this.state.average[3 + xiaoshi],
                YangHuaL_list: this.state.average[4 + xiaoshi],
                YangHuaT_list: this.state.average[5 + xiaoshi],
                YangHuaG_list: this.state.average[6 + xiaoshi],
                YangHuaMei_list: this.state.average[7 + xiaoshi],
                YangHuaMeng_list: this.state.average[8 + xiaoshi],
                TO_list: this.state.average[9 + xiaoshi],
                SanYangHL_list: this.state.average[10 + xiaoshi],
                HeJi: this.state.average[11 + xiaoshi],
                KH_list: this.state.average[12 + xiaoshi],
                n: this.state.average[13 + xiaoshi],
                P: this.state.average[14 + xiaoshi],

            },
            {
                time: '合格率',
                FuZhuoShui_list: '--',
                BuRongWu_list: '--',
                LL_list: '--',
                ErYangHG_list: '--',
                YangHuaL_list: '--',
                YangHuaT_list: '--',
                YangHuaG_list: '--',
                YangHuaMei_list: '--',
                YangHuaMeng_list: '--',
                TO_list: '--',
                SanYangHL_list: '--',
                HeJi: '--',
                KH_list: this.state.passRate[12 + xiaoshi],
                n: this.state.passRate[13 + xiaoshi],
                P: this.state.passRate[14 + xiaoshi],
            }
        );

        return (
            <Fragment>
                <div style={this.props.style}>
                    <h1 align="center">{this.props.title}</h1>
                    <span style={{float: "center", font: '20px', paddingLeft: 20}}>日期：{this.props.riqi}</span>
                    <span
                        style={{float: "center", font: '20px', paddingLeft: 20}}>班次：{this.props.timeChose * 8}点班</span>
                    <div className="upper">
                        {/*表格填写*/}
                        <Table style={this.props.style} columns={columns} bordered dataSource={data}
                               pagination={false}/>
                    </div>
                </div>

            </Fragment>
        );
    }

}

export default CKslyclhxfxbgd;